<template>
	<view class="pageBox">
		<image :src="dataInfo.fileUrl" v-if="dataInfo.fileUrl" class="topBG"></image>
		<image src="@/static/common/talentApartment_topBG1.png" v-else class="topBG"></image>
		<view class="contentBoxF">
			<view class="addressBox">
				<view class="title">{{dataInfo.title}}</view>
				<view class="text">地址：{{dataInfo.address}}</view>
				<view class="text"><text>电话：</text><text class="blue">{{dataInfo.phone}}</text><uni-icons
						type="phone-filled" color="#1145D5" size="30rpx"></uni-icons></view>
			</view>
			<view class="contentBox">
				<view class="rowBlock">
					<text class="title">
						详情介绍
						<text class="titleX"></text>
					</text>
					<view class="text">
						<mp-html :content="dataInfo.details||dataInfo.remark"></mp-html>
					</view>
				</view>
			</view>
		</view>
		<view class="btnBox">
			<view class="submitBtn" @click="goto('common/talentApartmentList/talentApartmentList')">公寓列表</view>
		</view>
		<u-modal class="modalClass" :show="show" :zoom="false" :showConfirmButton="false">
			<view class="modalBox">
				<view class="titleBox">
					<view class="title">入住申请</view>
					<view class="closeBtn" @click="closeModal">
						<uni-icons type="closeempty" size="48rpx"></uni-icons>
					</view>
				</view>
				<view class="modalContentBox">
					<view class="modalRow">
						<view class="w50">
							<view class="rowTitle">申请人</view>
							<view class="rowText">{{userInfo.nickName}}</view>
						</view>
						<view class="w50">
							<view class="rowTitle">联系方式</view>
							<view class="rowText">{{userInfo.phonenumber}}</view>
						</view>
					</view>
					<view class="modalRow">
						<view class="w100">
							<view class="rowTitle">身份证号</view>
							<view class="rowText">{{userInfo.nickName}}</view>
						</view>
					</view>
					<view class="modalRow">
						<view class="w100">
							<view class="rowTitle">人才类型</view>
							<view class="rowText">{{eliteInfo.eliteType}}类</view>
						</view>
					</view>
					<u--form :model="formData" ref="uForm" :rules="rules" labelWidth="80">
						<!-- <u-form-item ref="" label="小区" prop="houseId" required>
							<view class="inputView u--border u--input u-border u-input"
								@click="openPicker(houseList,'houseId','title')">
								<text v-if="!formData.houseId" class="placeholder">请选择小区</text>
								<text v-else>{{formData['houseIdLabel']}}</text>
							</view>
						</u-form-item>
						<u-form-item ref="" label="楼栋" prop="buildingId" v-if="formData.houseId" required>
							<view class="inputView u--border u--input u-border u-input"
								@click="openPicker(buildingList,'buildingId','buildingName')">
								<text v-if="!formData.buildingId" class="placeholder">请选择楼栋</text>
								<text v-else>{{formData['buildingIdLabel']}}</text>
							</view>
						</u-form-item>
						<u-form-item ref="" label="房号" prop="apartmentId" v-if="formData.buildingId" required>
							<view class="inputView u--border u--input u-border u-input"
								@click="openPicker(apartmentList,'apartmentId','roomNumber')">
								<text v-if="!formData.apartmentId" class="placeholder">请选择房号</text>
								<text v-else>{{formData['apartmentIdLabel']}}</text>
							</view>
						</u-form-item>
						<u-form-item ref="" label="租赁时间" prop="applicationDate" required>
							<view class="inputView u--border u--input u-border u-input"
								@click="openDatetime('applicationDate')">
								<text v-if="!formData.applicationDate" class="placeholder">请选择租赁时间</text>
								<text v-else>{{dateFormat(formData.applicationDate)}}</text>
							</view>
						</u-form-item> -->
						<u-form-item ref="" label="住户数量" prop="households" required>
							<u--input type="number" v-model="formData.households" placeholder="请输入住户数量"></u--input>
						</u-form-item>
						<view class="btn" @click="submit">立即申请</view>
					</u--form>
				</view>
			</view>
		</u-modal>
		<u-picker :show="pickerShow" :columns="pickerData" :keyName="keyName" @confirm="pickerConfirm"
			@close="pickerClose" @cancel="pickerClose"></u-picker>
		<u-datetime-picker :minDate="-4607395543000" ref="datetimePicker" :show="datetimeShow" v-model="datetimeValue"
			mode="date" @confirm="datetimeConfirm" @close="closePicker('datetimeShow')"
			@cancel="closePicker('datetimeShow')"></u-datetime-picker>
		<u-modal class="rzShow" :show="rzShow" title="提示" content="请先提交认证申请成为人才!" confirmText="去提交" showCancelButton
			cancelText="取消" @confirm="toRZ" @close="RZX" @cancel="RZX"></u-modal>
	</view>
</template>

<script>
	import * as util from "@/utils/util.js";
	import * as roomApi from "@/api/room.js"
	import * as applicationsApi from "@/api/applications.js"
	import * as buildingApi from "@/api/building.js"
	import * as apartmentApi from "@/api/apartment.js"
	import {
		applications
	} from "@/api/autth"
	export default {
		data() {
			return {
				rzShow: false,
				infoId: '',
				dataInfo: {
					phone: '',
					area: 0,
					bathrooms: 0,
					bedrooms: 0,
					createBy: null,
					createTime: "",
					description: "",
					fileUrl: "",
					furnished: "",
					houseId: '',
					parking: "",
					petFriendly: "",
					remark: "",
					rentalPrice: 0,
					salePrice: 0,
					shared: "",
					status: "",
					title: "",
					type: "",
					updateBy: null,
					updateTime: "",
				},
				value: 1,
				pickerData: [],
				pickerShow: false,
				datetimeShow: false,
				show: false,
				formData: {
					houseId: '',
					houseIdLabel: '',
					buildingId: '',
					buildingIdLabel: '',
					apartmentId: '',
					apartmentIdLabel: '',
					applicationDate: ''
				},
				formRuls: {
					phone: '',
					wx: '',
					height: '',
					weight: ''
				},
				openData: '',
				bgColor: ['#D0E7FE', '#B1F7ED', '#FDE2C5', '#FDF4C5'],
				btnBGs: ['linear-gradient( 90deg, #5F96E8 0%, #1977E1 100%)',
					'linear-gradient( 90deg, #03D8AF 0%, #00A57D 100%)',
					'linear-gradient( 90deg, #F2B018 0%, #F57E01 100%)',
					'linear-gradient( 90deg, #F2B018 0%, #F57E01 100%)'
				],
				userInfo: null,
				eliteInfo: {
					eliteType: ''
				},
				pickerDataName: '',
				keyName: 'label',
				keyValue: 'houseId',

				columns: [],
				houseList: [],
				buildingList: [],
				// 公寓表格数据
				apartmentList: [],
				openName: '',
				datetimeValue: Number(new Date()),
				rules: {
					'houseId': {
						type: 'number',
						required: true,
						message: '请选择小区',
						trigger: ['blur', 'change']
					},
					'buildingId': {
						type: 'number',
						required: true,
						message: '请选择楼栋',
						trigger: ['blur', 'change']
					},
					'apartmentId': {
						type: 'number',
						required: true,
						message: '请选择房号',
						trigger: ['blur', 'change']
					},
					'applicationDate': {
						type: 'number',
						required: true,
						message: '请选择租赁时间',
						trigger: ['blur', 'change']
					},
					'households': {
						type: 'number',
						required: true,
						message: '请输入住户数量',
						trigger: ['blur', 'change']
					},
				},
			}
		},
		onLoad(options) {
			console.log(options);
			this.infoId = options.id
			this.getInfo()
			this.getHouseList()

		},
		onShow() {
			this.userInfo = this.$store.getters.my_userInfo
			if (!this.userInfo) {
				this.$store.dispatch('userBaseInfo').then(res => {
					this.userInfo = res.user
					this.eliteInfo = res.eliteInfo
				})
			}
		},
		methods: {
			goto(url) {
				this.$navTo(`pages/${url}`)
			},
			toRZ() {
				uni.$u.debounce(() => {
					this.$navTo(`pages/common/authentication/authentication`)
					this.rzShow = false
				}, 500)
			},
			RZX() {
				this.rzShow = false
			},
			authTip() {
				applications().then(res => {
					// uni.showToast({
					// 	mask: true,
					// 	title: '无权限'
					// })
					return res.data
				})
			},
			submit() {
				const that = this
				const formData = JSON.parse(JSON.stringify(that.formData))
				// formData.applicationDate = this.dateFormat(formData.applicationDate)
				formData.households = Math.ceil(formData.households)

				that.$refs.uForm.validate().then(res => {
					that.addSubmit(formData)
				}).catch(errors => {
					console.log(errors);
				})
			},
			closePicker(showName) {
				this[showName] = false
				this.openName = ''
			},
			dateFormat(time, format = "YYYY-mm-dd") {
				return util.dateFormat(time, format)
			},
			openDatetime(name) {
				this.datetimeShow = true
				this.openName = name
			},
			datetimeConfirm({
				mode,
				value
			}) {
				const openName = this.openName
				// this.$set(this.formData, openName, value)
				this.formData[openName] = value
				this.datetimeShow = false
				console.log(this.formData[openName]);
			},
			formatter(type, value) {
				if (type === 'year') {
					return `${value}年`
				}
				if (type === 'month') {
					return `${value}月`
				}
				if (type === 'day') {
					return `${value}日`
				}
				return value
			},
			/** 查询房源列表 */
			getApartmentList() {
				const params = {
					houseId: this.formData.houseId,
					buildingId: this.formData.buildingId,
					availability: 'N'
				}
				apartmentApi.apartmentList(params).then((res) => {
					this.apartmentList = res.rows
				})
			},
			/** 查询楼栋列表 */
			getBuildingList(houseId) {
				buildingApi.buildingList({
					houseId
				}).then((res) => {
					this.buildingList = res.rows
				})
			},
			/** 查询小区列表 */
			getHouseList() {
				roomApi.houseList().then((res) => {
					this.houseList = res.rows
				})
			},

			openPicker(data, name, keyName = 'title') {
				this.keyName = keyName
				this.pickerData = [data]
				this.pickerShow = true
				this.pickerDataName = name
			},
			getInfo() {
				roomApi.houseId(this.infoId).then(res => {
					console.log(res);
					if (typeof(res.data.fromJson) == 'string') {
						res.data.fromJson = JSON.parse(res.data.fromJson)
					}
					this.dataInfo = res.data
				})
			},
			// pickerConfirm(value) {
			// 	console.log(value);
			// 	const openData = this.openData
			// 	this.openData = value
			// 	this.pickerShow = false
			// },
			// pickerClose() {
			// 	this.pickerShow = false
			// },
			pickerConfirm({
				value
			}) {
				const pickerDataName = this.pickerDataName
				console.log(value[0], pickerDataName);
				this.pickerShow = false
				this.formData[pickerDataName + 'Label'] = value[0][this.keyName]
				this.formData[pickerDataName] = value[0][this.pickerDataName]
				if (pickerDataName == 'houseId') {
					this.getBuildingList(value[0].houseId)
				} else if (pickerDataName == 'buildingId') {
					this.getApartmentList()
				}
			},
			pickerClose() {
				this.pickerShow = false
				this.pickerDataName = ''
			},
			pickerCancel() {
				this.pickerShow = false
				this.pickerDataName = ''
			},
			change(e) {
				console.log("e:", e);
			},

			addSubmit(formData) {
				// const dataObj = {
				// 	houseId: this.infoId,
				// 	information: JSON.stringify(formJson)
				// }
				uni.$u.throttle(applicationsApi.addapplications(formData).then(res => {
					this.$toast('申请成功')
					this.closeModal()
				}), 500)
			},
			closeModal() {
				this.show = false
				let formData = this.formData
				let formRuls = this.formRuls
				for (let key in formData) {
					console.log(key);
					formData[key] = ''
				}
				for (let key in formRuls) {
					formRuls[key] = ''
				}
			},
			modalOpen() {
				applications().then(res => {
					if (res.data) {
						const that = this
						that.show = true
					}
				}).catch(err => {
					if (!err.data && this.userInfo.userType == 'yk') {
						this.rzShow = true
					} else {
						this.$toast('无权限')
					}
				})

				// that.$nextTick(() => {
				// 	that.$refs.formRef.initData(this.dataInfo.fromJson)
				// })
			}
		}
	}
</script>

<style lang="scss" scoped>
	@import "@/components/dynamicForms/dynamicForms.scss";

	.w50 {
		width: 50%;
		box-sizing: border-box;
	}

	.w100 {
		width: 100%;
		box-sizing: border-box;
	}

	/deep/ .u-modal__content {
		padding: 30rpx 0 0 0 !important;
	}

	.modalClass {
		/deep/ .u-transition {
			z-index: 10069 !important;
		}

	}

	.rzShow {
		/deep/ .u-modal__content {
			padding: 30rpx !important;
		}

	}

	.pageBox {

		.blue {
			color: #1145D5;
		}

		.topBG {
			width: 100%;
			height: 450rpx;
			object-fit: cover;
		}


		.addressBox {
			background: linear-gradient(180deg, #FFFFFF 0%, #FFFFFF 27%);
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: 2rpx solid #17D57A;
			padding: 30rpx;
			margin-bottom: 30rpx;

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: bold;
				font-size: 32rpx;
				color: #000000;
				letter-spacing: 2rpx;
				margin-bottom: 18rpx;
			}

			.text {
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #979797;
				margin-bottom: 18rpx;
			}
		}

		.contentBoxF {
			padding: 30rpx;
			min-height: calc(100vh - 450rpx - 124rpx - 88rpx - 160rpx);
			padding-bottom: 160rpx;
			position: relative;
			z-index: 2;
			background: linear-gradient(180deg, #FFFFFF 0%, #96D07C 100%);
		}

		.contentBox {
			background-color: #fff;
			border-radius: 16rpx 16rpx 16rpx 16rpx;
			border: 2rpx solid #17D57A;
			padding: 0 40rpx;
			padding-top: 60rpx;
			padding-bottom: 30rpx;

			.rowBlock {
				text-align: center;
				margin-bottom: 60rpx;
			}

			.title {
				font-family: PingFang SC, PingFang SC;
				font-weight: 600;
				font-size: 32rpx;
				color: #000000;
				position: relative;
			}

			.titleX {
				width: 100%;
				height: 4rpx;
				position: absolute;
				bottom: 0%;
				left: 0;
				background: #0CBC8E;
			}

			.text {
				font-family: PingFang SC, PingFang SC;
				font-weight: 500;
				font-size: 28rpx;
				color: #000000;
				margin-top: 40rpx;
				text-align: left;
			}
		}

		.btnBox {
			box-sizing: border-box;
			padding: 0 30rpx;
			height: 124rpx;
			background: #FFFFFF;
			border-radius: 0rpx 0rpx 0rpx 0rpx;
			position: fixed;
			width: 100%;
			z-index: 2;
			bottom: 0;
			left: 0;
			display: flex;
			align-items: center;
			justify-content: center;

			.submitBtn {
				width: 100%;
				display: flex;
				align-items: center;
				justify-content: center;
				font-family: PingFang SC, PingFang SC;
				font-weight: 400;
				font-size: 28rpx;
				color: #FFFFFF;
				height: 76rpx;
				background: linear-gradient(90deg, #5F96E8 0%, #1977E1 100%);
				border-radius: 40rpx 40rpx 40rpx 40rpx;
			}
		}



		.modalBox {
			width: 100%;
			padding-bottom: 40rpx;

			.titleBox {
				display: flex;
				justify-content: space-between;
				padding-bottom: 30rpx;
				border-bottom: 2rpx solid #F1EEEE;
				margin: 0 30rpx;

				.title {
					width: 100%;
					font-family: PingFang SC, PingFang SC;
					font-weight: 500;
					font-size: 32rpx;
					color: #000000;
				}

				.closeBtn {
					flex-shrink: 0;
				}
			}

			.modalContentBox {
				padding: 0 30rpx;

				.btn {
					display: flex;
					justify-content: center;
					align-items: center;
					height: 76rpx;
					background: linear-gradient(90deg, #5F96E8 0%, #1977E1 100%);
					border-radius: 40rpx 40rpx 40rpx 40rpx;
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #FFFFFF;
					margin-top: 40rpx;
				}

				.modalRow {
					display: flex;
					justify-content: space-between;
					align-items: flex-start;
					margin-bottom: 30rpx;

					&:first-of-type {
						margin-top: 30rpx;
					}
				}

				.modalRowTip {
					display: flex;
					align-items: center;
					height: 40rpx;
					color: #F00000;
					font-size: 24rpx;
					padding-left: 150rpx;

				}

				.modalInputRow {
					display: flex;
					align-items: center;

					.label {
						font-family: PingFang SC, PingFang SC;
						font-weight: 400;
						font-size: 28rpx;
						color: #000000;
						width: 138rpx;
						flex-shrink: 0;
					}

					.inputBox {
						width: 100%;
						height: 88rpx;
						background: #FFFFFF;
						border-radius: 16rpx;
						border: 2rpx solid #DADADA;
						box-sizing: border-box;
						display: flex;
						align-items: center;

						&.err {
							border-color: #F00000;
						}

						.inputSuffix {
							padding-right: 20rpx;
							flex-shrink: 0;
							font-family: PingFang SC, PingFang SC;
							font-weight: 400;
							font-size: 28rpx;
							color: #979797;
						}

						.input {
							padding: 0 20rpx;
							width: 100%;
							height: 98%;
						}
					}
				}

				.rowTitle {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #979797;
					margin-bottom: 10rpx;
				}

				.rowText {
					font-family: PingFang SC, PingFang SC;
					font-weight: 400;
					font-size: 28rpx;
					color: #000000;
				}
			}
		}


	}
</style>